// pages/my/my.js
import { My } from '../../utils/url.js';
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo: {},
      hasUserInfo: true,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      curIndex:0,
      waitpayNull:false,
      waitreceivedNull: false,
      receivedNull:false,
      allordersNull:false,
      waitpay:[],
      hasget:[],
      num1:'',
      num2:'',
      message:1,
      orderDetailbox:false,
  },
  callthePhone:function(e){
      let phone = e.currentTarget.dataset.phone;
      wx.makePhoneCall({
          phoneNumber: phone ,
      })
  },
//   点击查看订单详情
  orderTotal:function(e){
      let sheet = e.currentTarget.dataset.sheet,
              i = e.currentTarget.dataset.index,//获取数组的下标
              waitpay = this.data.waitpay,
              orderDetailbox = this.data.orderDetailbox,
              that  = this; 
      orderDetailbox = !orderDetailbox;
      that.setData({
          orderDetailbox:orderDetailbox
      })
      console.log(waitpay)
      console.log(orderDetailbox)
      if (orderDetailbox){
          wx.login({
              success: function (res) {
                  wx.request({
                      url: My.more,
                      method: 'get',
                      data: {
                          code: res.code,
                          order_sheet: sheet,
                      },
                      header: {
                          'content-type': 'application/json' // 默认值
                      },
                      success: function (res) {
                          console.log(res.data)
                          that.setData({
                              detailbox: res.data.result
                          })
                      }
                  })
              }
          });
      }       
  },
  //点击关闭图片关闭
  orderdetailClose: function(e){
        this.setData({
            orderDetailbox:false
        })
    },
//   取消订单
cancelOrder:function(e){
    let sheet = e.currentTarget.dataset.sheet;
    let index = e.currentTarget.dataset.index;
    let waitpay = this.data.waitpay;
    let num1 = this.data.num1;
    let that = this;
    wx.showModal({
        title: '提示',
        content: '亲爱的客官，您确定要取消订单吗？',
        success: function (res) {
            if (res.confirm) {
                if (num1 > 1) {
                    num1 = num1 - 1;
                } else {
                    num1 = '';
                }
                console.log('用户点击确定')
                console.log(waitpay)
                console.log(num1)
                waitpay.splice(index, 1);
                if (waitpay.length > 0){
                    that.setData({
                        waitpayNull:false,
                        waitpay: waitpay,
                        num1: num1,
                    })
                }else{
                    that.setData({
                        waitpayNull: true,
                        num1:'',
                    })
                }

                wx.login({
                    success: function (res) {
                        wx.request({
                            url: My.del,
                            method: 'post',
                            data: {
                                code: res.code,
                                order_sheet: sheet,
                            },
                            header: {
                                'content-type': 'application/json' // 默认值
                            },
                            success: function (res) {
                                console.log(res.data)
                            }
                        })
                    }
                });

            } else if (res.cancel) {
                console.log('用户点击取消')
            }
        }
    })
    
},
// 立即付款
buyNow:function(e){
    let sheet = e.currentTarget.dataset.sheet;
    let index = e.currentTarget.dataset.index;
    let totalMoney = e.currentTarget.dataset.money;
    let waitpay = this.data.waitpay;
    let num1 = this.data.num1;
    let that = this;
    num1 = num1 - 1;
    wx.login({
        success: function (res) {
            wx.request({
                url: My.rightnow,
                method: 'post',
                data: {
                    code: res.code,
                    order_sheet: sheet,
                    totalMoney: totalMoney,
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    if (res.data.status == "0") {
                        wx.requestPayment({
                            'timeStamp': res.data.result.opts.timeStamp,
                            'nonceStr': res.data.result.opts.nonce_str,
                            'package': res.data.result.opts.prepay_id,
                            'signType': res.data.result.opts.signType,
                            'paySign': res.data.result.opts.paySign,
                            'success': function (res) {
                                console.log('支付成功啦！')
                                waitpay.splice(index, 1);
                                if (waitpay.length == 0){
                                    that.setData({
                                        waitpayNull: true,
                                        num1: '',
                                    })    
                                }else{
                                    that.setData({
                                        waitpayNull:false,
                                        waitpay: waitpay,
                                        num1: num1,
                                    })
                                }
                               
                                wx.request({
                                    url: My.notpay,
                                    method: 'get',
                                    data: {
                                        code: res.code,
                                        searchType: "notget",
                                    },
                                    header: {
                                        'content-type': 'application/json' // 默认值
                                    },
                                    success: function (res) {
                                        console.log(res.data)
                                        if (res.data.result.list.length > 0) {
                                            let returnArray = res.data.result.list,
                                                num2 = 0;
                                            for (let i = 0; i <= returnArray.length - 1; i++) {
                                                let sum = 0,
                                                    num = 0;
                                                for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                                    sum += returnArray[i].tmp[j].discPrice * 1;
                                                    num += returnArray[i].tmp[j].num;
                                                }
                                                num2 = num2 + 1;
                                                returnArray[i].totalprice = sum;
                                                returnArray[i].totalnum = num;
                                            }
                                            console.log(returnArray)
                                            that.setData({
                                                num2: num2,
                                            })
                                        }
                                    }
                                })
                                    
                               
                            },
                            'fail': function (res) {
                                console.log('支付失败啦！')
                            },
                        })
                    }
                }
            })
        }
    });
},
//   未付款
waitpay:function(e){
    let that = this;
    this.setData({
        curIndex: 0
    })
    wx.login({
        success: function (res) {
            wx.request({
                url: My.notpay,
                method: 'get',                 
                data: {
                    code:res.code,
                    ifPay:"false",
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    if (res.data.result.list.length > 0){
                      let returnArray = res.data.result.list,
                            num1 = 0;
                        for (let i = 0; i <= returnArray.length - 1; i++) {
                            let sum = 0,
                                num = 0;
                            for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                sum += returnArray[i].tmp[j].discPrice * 1;
                                num += returnArray[i].tmp[j].num;
                            }
                            num1 = num1 + 1;
                            returnArray[i].totalprice = sum;
                            returnArray[i].totalnum = num;
                        }
                        console.log(returnArray)
                        that.setData({
                            waitpayNull: false,
                            waitpay: returnArray,
                            num1: num1,
                        })
                    }else{
                        that.setData({
                            waitpayNull:true,
                            num1:'',
                        })
                    }
                }
            })
        }
    });
},
// 待收货
waitreceived: function (e) {
    let that = this;
    this.setData({
        curIndex:1
    })
    wx.login({
        success: function (res) {
            wx.request({
                url: My.notpay,
                method: 'get',
                data: {
                    code: res.code,
                    ifPay: "true",
                    ifGet: "false"
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    if (res.data.result.list.length > 0) {
                      let returnArray = res.data.result.list,
                            num2 = 0;
                        for (let i = 0; i <= returnArray.length - 1; i++) {
                            let sum = 0,
                                num = 0;
                            for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                sum += returnArray[i].tmp[j].discPrice * 1;
                                num += returnArray[i].tmp[j].num;
                            }
                            num2 = num2 + 1;
                            returnArray[i].totalprice = sum;
                            returnArray[i].totalnum = num;
                        }
                        console.log(returnArray)
                       that.setData({
                           waitreceivedNull: false,
                                waitreceived: returnArray,
                                num2: num2,
                            })
                        
                        
                    } else {
                        that.setData({
                            waitreceivedNull: true,
                            num2:'',
                        })
                    }
                }
            })
        }
    });
},
// 已收货
received:function(e) {
    let that = this;
    this.setData({
        curIndex: 2
    })
    wx.login({
        success: function (res) {
            wx.request({
                url: My.notpay, 
                method: 'get',                
                data: {
                    code: res.code,
                    ifGet: "true"
                },
                method: 'get',
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    if (res.data.result.list.length > 0) {
                      let returnArray = res.data.result.list
                        for (let i = 0; i <= returnArray.length - 1; i++) {
                            let sum = 0,
                                num = 0;
                            for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                sum += returnArray[i].tmp[j].discPrice * 1;
                                num += returnArray[i].tmp[j].num;
                            }

                            returnArray[i].totalprice = sum;
                            returnArray[i].totalnum = num;
                        }
                        console.log(returnArray)
                        that.setData({
                            receivedNull: false,
                            hasget: returnArray,
                        })
                    } else {
                        that.setData({
                            receivedNull: true,
                        })
                    }
                }
            })
        }
    });
},
// 全部订单
allorders:function(e) {
    let that = this;
    this.setData({
        curIndex: 3
    })
    wx.login({
        success: function (res) {
            wx.request({
                url: My.notpay, 
                method: 'get',                
                data: {
                    code: res.code,
                    // searchType: "all"
                },
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: function (res) {
                    console.log(res.data)
                    if (res.data.result.list.length > 0) {
                      let returnArray = res.data.result.list
                        for (let i = 0; i <= returnArray.length - 1; i++) {
                            let sum = 0,
                                num = 0;
                            for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                sum += returnArray[i].tmp[j].discPrice * 1;
                                num += returnArray[i].tmp[j].num;
                            }

                            returnArray[i].totalprice = sum;
                            returnArray[i].totalnum = num;
                        }
                        console.log(returnArray)
                        that.setData({
                            allordersNull: false,                            
                            allorders:  returnArray,
                        })


                    } else {
                        that.setData({
                            allordersNull: true,
                        })
                    }
                }
            })
        }
    });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
      var that = this;
      //初始化页面
      this.setData({
          curIndex:0,
      })
      if (app.globalData.userInfo) {
          this.setData({
              userInfo: app.globalData.userInfo,
              hasUserInfo: true
          })
      } else if (this.data.canIUse) {
          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          app.userInfoReadyCallback = res => {
              this.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true
              })
          }
      } else {
          // 在没有 open-type=getUserInfo 版本的兼容处理
          wx.getUserInfo({
              success: res => {
                  app.globalData.userInfo = res.userInfo
                  this.setData({
                      userInfo: res.userInfo,
                      hasUserInfo: true
                  })
              }
          })
      }
      wx.login({
          success: function (res) {
              wx.request({
                  url: My.notpay,
                  method: 'get',
                  data: {
                    code: res.code,
                    ifPay: "false",
                      // searchType: "notpay",
                  },
                  header: {
                      'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    console.log(res.data.result.list.length)                    
                      if (res.data.result.list.length > 0 ) {
                          that.setData({
                              waitpayNull: false,
                          })
                          let returnArray = res.data.result.list,
                                num1 = 0;
                          for (let i = 0; i <= returnArray.length - 1; i++) {
                              let sum = 0,
                                  num = 0;
                                //   orderDetailbox = false;
                              for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                  sum += returnArray[i].tmp[j].discPrice * 1;
                                  num += returnArray[i].tmp[j].num;
                              }
                              returnArray[i].totalprice = sum;
                              returnArray[i].totalnum = num;
                                num1 = num1 + 1;
                          }
                          console.log(returnArray)  
                          that.setData({
                              num1:num1,
                              waitpay: returnArray,
                              // orderdetails: res.data.returnArray[0],
                          })
                      } else {
                          that.setData({
                              waitpayNull: true,
                              num1:'',
                          })
                      }
                  }
              })
          }
      });
      // wx.login({
      //     success: function (res) {
      //         wx.request({
      //             url: My.notpay,
      //             method: 'get',
      //             data: {
      //                 code: res.code,
      //                 searchType: "notget",
      //             },
      //             header: {
      //                 'content-type': 'application/json' // 默认值
      //             },
      //             success: function (res) {
      //                 console.log(res.data)
      //                 if (res.data.return_code) {
      //                     let returnArray = res.data.returnArray,
      //                         num2 = 0;
      //                     for (let i = 0; i <= returnArray.length - 1; i++) {
      //                         let sum = 0,
      //                             num = 0;
      //                         for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
      //                             sum += returnArray[i].tmp[j].discPrice;
      //                             num += returnArray[i].tmp[j].num;
      //                         }
      //                         num2 = num2 + 1;
      //                         returnArray[i].totalprice = sum;
      //                         returnArray[i].totalnum = num;
      //                     }
      //                     console.log(returnArray)
      //                     that.setData({
      //                         waitreceivedNull: false,
      //                         waitreceived: res.data.returnArray,
      //                         num2: num2,
      //                     })


      //                 } else {
      //                     that.setData({
      //                         waitreceivedNull: true,
      //                         num2: '',
      //                     })
      //                 }
      //             }
      //         })
      //     }
      // });
  },
  onShow: function () {
      var that = this;
      //初始化页面
      this.setData({
          curIndex: 0,
      })
      wx.login({
          success: function (res) {
              wx.request({
                  url: My.notpay,
                  method: 'get',
                  data: {
                      code: res.code,
                      searchType: "notpay",
                  },
                  header: {
                      'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                      console.log(res.data)
                      if (res.data.return_code) {
                          that.setData({
                              waitpayNull: false,
                          })
                          let returnArray = res.data.result.list,
                              num1 = 0;
                          for (let i = 0; i <= returnArray.length - 1; i++) {
                              let sum = 0,
                                  num = 0;
                              //   orderDetailbox = false;
                              for (let j = 0; j <= returnArray[i].tmp.length - 1; j++) {
                                  sum += returnArray[i].tmp[j].discPrice * 1;
                                  num += returnArray[i].tmp[j].num;
                              }
                              returnArray[i].totalprice = sum;
                              returnArray[i].totalnum = num;
                              num1 = num1 + 1;
                          }
                          console.log(res.data.returnArray)
                          that.setData({
                              num1: num1,
                              waitpay: res.data.returnArray,
                              // orderdetails: res.data.returnArray[0],
                          })
                      } else {
                          that.setData({
                              waitpayNull: true,
                              num1: '',
                          })
                      }
                  }
              })
          }
      });
  },
  getUserInfo: function (e) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
          userInfo: e.detail.userInfo,
          hasUserInfo: false
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
//   onReachBottom: function () {
  
//   },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})